# This test shows the asynchronous use of an mq server
import time
from mediadart.mqueue.mqclient_async import Proxy

responses = 0
XRANGE=YRANGE=11

def print_cb(status_ok, result, n):
    global responses, t0, XRANGE, YRANGE
    responses += 1
    if status_ok:
        print 'OK: %s = %s'  % (n, result)
    else:
        print 'ERROR: %s: error %s' % (n, str(result))
    if responses >= XRANGE*YRANGE:
        t1 = time.time()
        print 'elapsed = %.3f' % (t1-t0)


#try:
#    print 'describe', ab.proxy('Calculator').system.describe()
#except Exception, e:
#    print e

#p = ab.proxy('Calculator')
t0 = time.time()
for i in xrange(XRANGE):
    for j in xrange(YRANGE):
        label = '%d*%d' % (i, j) 
        print 'requesting %s' % label
## Non blocking version
        Proxy('Calculator', print_cb, label).prod(i, j)

## Blocking version
#        print '%d*%d =' % (i, j), ab.proxy('Calculator').prod(i, j)
#t1 = time.time()
#print 'elapsed = %.3f' % (t1-t0)


